//
//  C_BaseMovePlayerViewController.m
//  音悦咖
//
//  Created by Best on 15/7/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "C_BaseMovePlayerViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "C_MovieListTableViewCell.h"
#import "L_Singleton.h"

#define API_LIST_URL_FOUNT       @"http://api.dongting.com/sim/mv/"
#define API_LIST_URL_BACK      @"/similarity?app=ttpod&v=v7.9.4.2015052918&uid=&mid=iPhone4S&f=f320&s=s310&imsi=&hid=&splus=8.0&active=1&net=2&openudid=b55e125b1cbcd2f8d637e5573f9a6f6f241e0c3f&idfa=DD02B768-7FEA-48C3-A977-931093C926A0&utdid=VZ5%2FNAYMGNwDAGGSKGx50btO&alf=201200&bundle_id=com.ttpod.music"


@interface C_BaseMovePlayerViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray *listArr;

@end

@implementation C_BaseMovePlayerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.leftButton addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.rightButton.hidden = YES;
    [self getdata];
}

#pragma mark 模态返回
- (void)buttonAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


#pragma mark 创建视频

- (void)createMoviePlayerView
{
    moviePlayerView = [[C_MoviePlayerView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, 240) MovieURL:[NSURL URLWithString:[self.model.mvList[0] objectForKey:@"url"]]];
    [moviePlayerView.movieController play];
    
    moviePlayerView.model = self.model;
    moviePlayerView.movieController.scalingMode = MPMovieScalingModeFill;
    L_Singleton *single = [L_Singleton shareManager];
    [single.streamer stop];
  
    [moviePlayerView.movieController setFullscreen:YES animated:YES];
    
    [self.view addSubview:moviePlayerView];
    


}
#pragma mark Getdata

- (void)getdata
{
       [AFNetworkHandler GETWithAFNByURL:self.movePlayer_url completion:^(id result) {
           
           self.model = [C_MoviesListModel baseModelWithDic:[result objectForKey:@"data"]];
        
           [self createMoviePlayerView];
           [self addbuttonAction];
           [self getListData];
    
       }];
}
#pragma mark 添加button方法
- (void)addbuttonAction
{
    for (NSInteger i = 0; i < 3; i++) {
        UIButton *button = (UIButton *) [moviePlayerView viewWithTag:3000 + i];
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    }
}


- (void)buttonAction:(UIButton *)button
{
    
   // NSLog(@"%d", button.tag);
    UIButton *test = [UIButton buttonWithType:(UIButtonTypeSystem)];
    test.selected = NO;
    button.selected = YES;
    button = test;
}


#pragma mark  GetlistData

- (void)getListData
{
      [AFNetworkHandler GETWithAFNByURL:self.movieList_url completion:^(id result) {
          
          self.listArr = [C_MoviesListModel arrayWithModelByArray:[result objectForKey:@"data"]];
          NSLog(@"%@", [self.listArr[0] mvList]);
          [self createListTableView];
      }];
}


#pragma mark getNewListData
- (void)getNewLlistData:(NSString *)movieId
{
    NSString *url = [NSString stringWithFormat:@"%@%@%@", API_LIST_URL_FOUNT, movieId, API_LIST_URL_BACK];
    
    [AFNetworkHandler GETWithAFNByURL:url completion:^(id result) {
         self.listArr = [C_MoviesListModel arrayWithModelByArray:[result objectForKey:@"data"]];
        
        [listTabelView reloadData];
    }];
}

#pragma mark CreateTableView
- (void)createListTableView
{
    listTabelView = [[UITableView alloc] initWithFrame:CGRectMake(0, moviePlayerView.frame.size.height + moviePlayerView.frame.origin.y, SCREEN_WIDTH, SCREEN_HEIGHT - moviePlayerView.frame.size.height + moviePlayerView.frame.origin.y) style:(UITableViewStyleGrouped)];
    listTabelView.delegate = self;
    listTabelView.dataSource = self;
    [self.view addSubview:listTabelView];
    [listTabelView release];
    
    [listTabelView registerClass:[C_MovieListTableViewCell class] forCellReuseIdentifier:@"C_MovieListTableViewCell"];
    listHeaderView.backgroundColor = [UIColor whiteColor];
    listHeaderView = [[C_ListHeaderView alloc] initWithFrame:CGRectMake(0, 0, listTabelView.frame.size.width, 100)];
    listHeaderView.model = self.model;
    listTabelView.tableHeaderView = listHeaderView;
   // [listHeaderView addSubview:listHeaderView];
    [listHeaderView release];
    
}


#pragma mark Tabel Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.listArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    C_MovieListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"C_MovieListTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.listArr[indexPath.row];
    [cell myLayout];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_WIDTH / 4;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self getNewLlistData:[self.listArr[indexPath.row] movieId]];
   moviePlayerView.model = self.model = self.listArr[indexPath.row];
    
    if (self.model.mvList.count) {
        [moviePlayerView.movieController stop];
        moviePlayerView.movieController.contentURL = [NSURL URLWithString:[self.model.mvList[0] objectForKey:@"url"]];
        [moviePlayerView.movieController play];
        listHeaderView.model = self.listArr[indexPath.row];
    
    }
  
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}


- (void)viewDidDisappear:(BOOL)animated
{
    [moviePlayerView.movieController stop];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
